/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2017-07-21 11:42:21
 * @Copyright: 2017 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
package com.itttq.gshop.pbpm.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.itttq.gshop.domain.po.SysSetPo;
import com.itttq.gshop.domain.to.ResultPageTo;
import com.itttq.gshop.pbpm.service.SysSetService;

@RestController
@RequestMapping("sysSet")
public class SysSetController extends GenericPbpmController {
	private static final long serialVersionUID = -2476967672313554L;
	public static final String CACHE_KEY = "sysSet";
	
	@Autowired
	private SysSetService sysSetService;
	
	/**
	 * 根据条件获取SysSetPo对象，不返回查询的数据总数
	 * 
	 * @param sysSet
	 * @Author 迷蝶
	 * @return ResultPageTo<SysSetPo>
	 */
	@RequestMapping("sets")
    public ResultPageTo<SysSetPo> getSets(@RequestBody SysSetPo sysSet) {
		ResultPageTo<SysSetPo> sets = null;
		List<SysSetPo> setList = null;
		if(MAP_SET.isEmpty() || StringUtils.isNotBlank(sysSet.getCode()) || StringUtils.isNotBlank(sysSet.getName())) {
			sets = sysSetService.getSysSets(sysSet);
			setList = sets.getData();
			for(SysSetPo set : setList) {
				MAP_SET.put(set.getCode(), set);
			}
		} else {
			// 此处暂定只能按code搜索，所以获取就从缓存获取
			setList = new ArrayList<SysSetPo>();
			int i = 0;
			for(String code : MAP_SET.keySet()) {
				if(i >= sysSet.getStart() && setList.size() != sysSet.getSize()) {
					setList.add(MAP_SET.get(code));
				}
			}
			sets = new ResultPageTo<SysSetPo>(sysSet.getStart(), sysSet.getSize(), MAP_SET.size(), setList);
		}
        return sets;
    }
	
	/**
	 * 根据条件获取SysSetPo对象，并返回查询的数据总数
	 * 
	 * @param sysSet
	 * @Author 迷蝶
	 * @return ResultPageTo<SysSetPo>
	 */
	@RequestMapping("list")
    public ResultPageTo<SysSetPo> getSysSets(@RequestBody SysSetPo sysSet) {
		ResultPageTo<SysSetPo> sets = null;
		List<SysSetPo> setList = null;
		if(MAP_SET.isEmpty() || StringUtils.isNotBlank(sysSet.getCode()) || StringUtils.isNotBlank(sysSet.getName())) {
			sets = sysSetService.getSysSets(sysSet);
			setList = sets.getData();
			for(SysSetPo set : setList) {
				MAP_SET.put(set.getCode(), set);
			}
		} else {
			// 此处暂定只能按code搜索，所以获取就从缓存获取
			setList = new ArrayList<SysSetPo>();
			int i = 0;
			for(String code : MAP_SET.keySet()) {
				if(i >= sysSet.getStart() && setList.size() != sysSet.getSize()) {
					setList.add(MAP_SET.get(code));
				}
			}
			sets = new ResultPageTo<SysSetPo>(sysSet.getStart(), sysSet.getSize(), MAP_SET.size(), setList);
		}
        return sets;
    }
	
	/**
	 * 根据ID获取SysSetPo对象，并放入redis缓存
	 * 
	 * @param id	只能是数字
	 * @Author 迷蝶
	 * @return SysSetPo
	 */
	@RequestMapping("/{code}")
	@Cacheable(value=CACHE_KEY, key="'sysSet_'+#code")
	public SysSetPo getSysSet(@PathVariable("code") String code) {
		return sysSetService.getSysSet(code);
	}
	
	/**
	 * 保存SysSetPo对象，并清除此ID的缓存。<br>
	 * 
	 * @param sysSet
	 * @Author 迷蝶
	 * @return SysSetPo
	 */
	@RequestMapping("/save")
	@CacheEvict(value=CACHE_KEY, key="'sysSet_'+#sysSet.code")
	public Integer saveSysSet(@RequestBody SysSetPo sysSet) {
		int rs = 0;
		if(null == sysSet) {return rs;}
		if(null == MAP_SET.get(sysSet.getCode())) {
			rs = sysSetService.addSysSet(sysSet);
		} else {
			rs = sysSetService.updateSysSet(sysSet);
		}
		// 加入缓存，TODO GXG 改为redis，不然分布式部署会有问题
		MAP_SET.put(sysSet.getCode(), sysSet);
		return rs;
	}
	
	/**
	 * 根据ID删除SysSetPo对象，并清除此ID的缓存。<br>
	 * 
	 * @param code	只能是数字
	 * @Author 迷蝶
	 * @return SysSetPo
	 */
	@RequestMapping("/del/{code}")
	@CacheEvict(value=CACHE_KEY, key="'sysSet_'+#code")
	public Integer delSysSet(@PathVariable("code") String code) {
		int rs = sysSetService.delSysSet(code);
		// 加入缓存，TODO GXG 改为redis，不然分布式部署会有问题
		if (rs > 0) {
			MAP_SET.remove(code);
		}
		return rs;
	}
}